package com.cd.university.common;

import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一的响应格式
 * @author 陈栋
 * @create 2021/9/2 21:59
 */
public class RestResult extends HashMap<String ,Object> {
    private String code;
    private String msg;
    private Object data;

    public RestResult() {
        put("code", HttpStatus.OK.value());
        put("msg","success");
    }

    @Override
    public RestResult put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public static RestResult success() {
        return new RestResult();
    }

    public static RestResult success(String msg) {
        RestResult restResult = new RestResult();
        restResult.put("msg",msg);
        return restResult;
    }

    public static RestResult success(String msg, Object data) {
        RestResult restResult = new RestResult();
        restResult.put("msg",msg);
        restResult.put("data",data);
        return restResult;
    }

    public static RestResult success(Map<String ,Object> map) {
        RestResult restResult = new RestResult();
        restResult.putAll(map);
        return restResult;
    }

    public static RestResult failure(int code,String msg) {
        RestResult restResult = new RestResult();
        restResult.put("msg",msg);
        restResult.put("code",code);
        return restResult;
    }

    public static RestResult failure(String msg) {
        return failure(HttpStatus.EXPECTATION_FAILED.value(),msg);
    }

    public static RestResult failure() {
        return failure(HttpStatus.EXPECTATION_FAILED.value(),"服务器内部错误，请检查bug！");
    }
}
